from py2neo.ogm import GraphObject, Property, RelatedTo, RelatedFrom, Label
from py2neo import Node, Relationship,Graph

from common.sql_define import project_info_sql,project_contact_sql
from db_utils.mysql_utils import MysqlInstance

from db_utils.dict_utils import split_values_from_keys,remove_None_value,make_neo4j_name
from common.sql_dict import convert_dict_entiprise,convert_dict_contactor,convert_dict_project,neo4j_name_list,convert_zhongbiao_relation
from pprint import  pprint
graph = Graph('http://localhost:7474', username='neo4j', password='123456')
mysql_obj = MysqlInstance()
results = mysql_obj.execute_sql(project_info_sql)
neo4j_nodes = []
enpersiter_nodes_dict = {}
contact_nodes_dict = {}
relation_nodes_dict = {}



def check_exist(info_dict,note_save_dict,nodel_label):
    if info_dict['name'] in note_save_dict:
        node = note_save_dict[info_dict['name']]
        exist = 1
    else:
        node = Node(nodel_label, **enprise_info)
        note_save_dict[info_dict['name']] = node
        exist = 0
    return node,exist

def check_exist_relation(note_save_dict,nodel_label,p,q,node_info=None):
    check_key = p['name']+"_"+q['name']
    if check_key in note_save_dict:
        node = note_save_dict[check_key]
        exist = 1
    else:
        node = Relationship(enterprise, nodel_label, project)
        if node_info != None:
            for key,value in node_info.items():
                node[key] = value
        exist = 0
    return node,exist

for result in results:
    pprint(result)

    neo4j_nodes = []
    remove_None_value(result)

    project_info  = split_values_from_keys(result,convert_dict_project)
    enprise_info = split_values_from_keys(result,convert_dict_entiprise)
    contactor_info = split_values_from_keys(result,convert_dict_contactor)
    zhongbiao_relation_info = split_values_from_keys(result,convert_zhongbiao_relation)


    make_neo4j_name(project_info,neo4j_name_list)
    make_neo4j_name(enprise_info,neo4j_name_list)
    make_neo4j_name(contactor_info,neo4j_name_list)
    
    
    project = Node("工程项目", **project_info)
    neo4j_nodes.append(project)

    enterprise,exist = check_exist(enprise_info,enpersiter_nodes_dict,'企业')
    if not exist:
        neo4j_nodes.append(enterprise)
        

    #if contactor_info['name'] in enpersiter_nodes_dict:
    contactor = Node("项目负责人",**contactor_info)
    if not graph.exists(contactor):
        neo4j_nodes.append(contactor)

    contactor, exist = check_exist(contactor_info, contact_nodes_dict, '企业')
    if not exist:
        neo4j_nodes.append(contactor)

    # 创建ab之间的关系
    toubiao_relation,exist = check_exist_relation(relation_nodes_dict,"投标",enterprise,project)
    if not exist:
        neo4j_nodes.append(toubiao_relation)

    zhongbiaodanwei_relation,exist = check_exist_relation(relation_nodes_dict,"中标单位",project,enterprise,zhongbiao_relation_info)
    if not exist:
        neo4j_nodes.append(zhongbiaodanwei_relation)

    zhongbiaodanweifuzeren_relation ,exist= check_exist_relation(relation_nodes_dict,"中标单位负责人",project,contactor)
    if not exist:
        neo4j_nodes.append(zhongbiaodanweifuzeren_relation)


    contactor_enterprise_relation,exist = check_exist_relation(relation_nodes_dict,"就职",contactor,enterprise)
    if not exist:
        neo4j_nodes.append(contactor_enterprise_relation)


    print(zhongbiaodanwei_relation)
    print(toubiao_relation)
    print(contactor_enterprise_relation)
    print(zhongbiaodanwei_relation)
    print(zhongbiaodanweifuzeren_relation)




    search_sql = project_contact_sql % result["crm_tender_announcement_id"]
    project_owner_info = mysql_obj.execute_sql(search_sql)
    print(project_owner_info)


    for info in project_owner_info:
        if info['enterprise_name']:
            tempdict = {}
            tempdict["enterprise_name"] = info["enterprise_name"]
            tempdict["name"] =  info["enterprise_name"]
            enterprise,exist = check_exist(tempdict, contact_nodes_dict, '企业')
            if not exist:
                neo4j_nodes.append(enterprise)

            temp_relation,exist = check_exist_relation(relation_nodes_dict, info['role'], project, enterprise)
            if not exist:
                neo4j_nodes.append(temp_relation)

    for s in neo4j_nodes:
        graph.create(s)


        graph.merge()





# 输出节点之间的关系：(Alice)-[:KNOWS]->(Bob)


